package com.library.dao;

import com.library.bean.ReaderCard;
import com.library.bean.ReaderInfo;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Repository
public class ReaderCardDao {

    @Resource
    private SqlSessionTemplate sqlSession;

    private final String namespace="com.library.dao.ReaderCardDao.";

    public int getIdMatchCount(String username, String passwd){
        Map<String,Object> map=new HashMap<>();
        map.put("username",username);
        map.put("password",passwd);
        return sqlSession.selectOne(namespace+"getIdMatchCount",map);

    }

    public ReaderCard findReaderCardByReaderId(String username) {
        return sqlSession.selectOne(namespace+"findReaderCardByReaderId",username);
    }

    public int addReaderCard(ReaderInfo readerInfo, String password) {
        String username=readerInfo.getName();
        long readerId=readerInfo.getReader_id();

        Map<String,Object> map=new HashMap<>();
        map.put("username",username);
        map.put("password",password);
        map.put("reader_id",readerId);
        return sqlSession.insert(namespace+"addReaderCard",map);
    }

    public int editReaderCard(ReaderInfo readerInfo) {
        return sqlSession.update(namespace+"editReaderCard",readerInfo);
    }

    public int deleteReaderCard(long readerId) {
        return sqlSession.delete(namespace+"deleteReaderCard",readerId);
    }

    public String getPasswd(long id) {
        return sqlSession.selectOne(namespace+"getPasswd",id);
    }

    public int resetPasswd(long id, String newPasswd) {
        Map<String,Object> map=new HashMap<>();
        map.put("reader_id",id);
        map.put("password",newPasswd);
        return sqlSession.update(namespace+"resetPasswd",map);
    }
}
